{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "383b1cc6",
   "metadata": {},
   "source": [
    "# 通用场景识别"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc81678e",
   "metadata": {},
   "source": [
    "## 通用文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "114546f9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.682fe94ce12c2455d07dd4a942eef7db.315360000.1965040432.282335-25904777', 'expires_in': 2592000, 'session_key': '9mzdCKJPjWZw/lYswHNXe5wqeayLG7a897y9dYle8ycZBhDVmG/j6bEdD22Ix4QjVYVjcHp2QoN19C+t0irARC//O6BC9A==', 'access_token': '24.3f8d24da91eb924a5d2a1b982e3e3493.2592000.1652272432.282335-25904777', 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage brain_multi_ object_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': 'e530c8b7c6a76f2006e7bda71a67bcf6'}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'24.3f8d24da91eb924a5d2a1b982e3e3493.2592000.1652272432.282335-25904777'"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests \n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=4dfdd0f637524590b976562a1f497c8b&client_secret=e5MRWHNpOtoVicw1DrDjuTA3AGD2wB0V'\n",
    "#准备URL参数\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'AblvqrCFNWG0IcGTVZwkTYOi',\n",
    "    'client_secret':'e5MRWHNpOtoVicw1DrDjuTA3AGD2wB0V'\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "if response:\n",
    "    print(response.json())\n",
    "token = response.json()[\"access_token\"]\n",
    "token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "8dfb7a41",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 6, 'error_msg': 'No permission to access data'}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用文字识别（高精度版）\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('fayu.jpeg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img,\n",
    "         \"access_token\":token,\n",
    "          'baike_num':5,\n",
    "          \"top_num\":5\n",
    "         }\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbaed82a",
   "metadata": {},
   "source": [
    "## 网络图片文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "c9caa110",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import base64\n",
    "def everything(token,image_path):\n",
    "    url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/webimage\"\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open(image_path, 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = {\n",
    "        \"image\":img,\n",
    "        \"access_token\": token\n",
    "         }\n",
    "    headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "    response = requests.post(url, data=params, headers=headers)\n",
    "    return response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "3cc52d90",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 6, 'error_msg': 'No permission to access data'}"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net_info=everything(token,'wangtu.jpeg')\n",
    "net_info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c1556f8",
   "metadata": {},
   "source": [
    "## 二维码识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "84a2a760",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import base64\n",
    "def everything(token,image_path):\n",
    "    url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode\"\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open(image_path, 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = {\n",
    "        \"image\":img,\n",
    "        \"access_token\": token\n",
    "         }\n",
    "    headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "    response = requests.post(url, data=params, headers=headers)\n",
    "    return response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "7fa5d0ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 6, 'error_msg': 'No permission to access data'}"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qr_word=everything(token,'qr.jpeg')\n",
    "qr_word"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "925590bc",
   "metadata": {},
   "source": [
    "# 卡证文字识别"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75610475",
   "metadata": {},
   "source": [
    "## 身份证或银行卡识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b159d9b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "741c0500",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dfc5b7c0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "53e2af60",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
